//
//  MsgDetailViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-7-1.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "MsgDetailViewController.h"
#import "NotificationCenter.h"

@interface MsgDetailViewController ()

@property(nonatomic,strong) IBOutlet UIWebView *webView;

@end

@implementation MsgDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:NSLocalizedString(@"通知详情", nil)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateInfo:)
                                                 name:showMsgInCenterNotification
                                               object:nil];
    //NSLog(@"viewDidLoad");
    
}

-(void)updateInfo:(NSNotification *)notes
{
    NSDictionary* message = [notes userInfo];
    
    //[self.titleLabel setText:[[notes userInfo] objectForKey:@"title"]];
    //[self.dateLabel setText:[[notes userInfo] objectForKey:@"date"]];
    //[self.contentView setText:[[notes userInfo] objectForKey:@"content"]];
    
    //读入html模板
    NSString *path = [[NSBundle mainBundle] pathForResource:@"webPage/msgdetail" ofType:@"html"];
    
    NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    
    NSString *string = [[NSString alloc] initWithData:
                        [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
    //设置内容
    NSString *pageTitle = message[@"title"];
    NSString *title = message[@"title"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"MM-dd HH:mm"];
    NSString *dateTime= [dateFormatter stringFromDate:message[@"date"]];
    //NSString *dateTime = message[@"date"];
    NSString *content =message[@"content"];
    
    NSString *newPageTitle = [string stringByReplacingOccurrencesOfString:@"${pageTitle}" withString:pageTitle];
    NSString *newTitle = [newPageTitle stringByReplacingOccurrencesOfString:@"${title}" withString:title];

    NSString *newDateTime = [newTitle stringByReplacingOccurrencesOfString:@"${dateTime}" withString:dateTime];
    NSString *newStr = [newDateTime stringByReplacingOccurrencesOfString:@"${content}" withString:content];
    //在webView上显示
    NSURL *mainBundleURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/webPage",[[NSBundle mainBundle] bundlePath]]];
    [self.webView loadHTMLString:newStr baseURL:mainBundleURL];

    
    
    NSLog(@"updateInfo");
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //NSLog(@"viewDidAppear");
    
}

-(void)leftButtonPress:(id)sender{
    
    [self.navigationController dismissViewControllerAnimated:YES completion:^{}];
}


@end
